<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Backend BoardsController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class Backend_BoardsController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	/*
	 * Init class vars and stuff
	 */
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	
	
	/*
	 * List boards
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		// db
		$categories = new Categories();
		
		// query
		$getCategories 	= $categories->fetchAll();
		
		// view
		$this->view->cats_count 	= $getCategories->count();
		$this->view->cats_res		= $getCategories->toArray();
	}
	
	
	
	/*
	 * Add a new board
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function addAction()
	{
		// vars
		$formData	= $this->_request->getPost();
		
		// db
		$boards		= new Boards();
		$groups		= new Groups();
		$categories	= new Categories();
		
		// form
		$form		= new BoardsForm();
		$form->removeElement('dump');
		$form->removeElement('is_deleted');
		
		// query
		$queryGroups		= $groups->fetchAll();
		$queryCategories	= $categories->fetchAll();

		// fill select-fields
		$selCategory = $form->getElement('category_uid');
		if($queryCategories->count() > 0)
		{
			foreach($queryCategories->toArray() AS $result)
			{
				$selCategory->addMultiOption($result['uid'], $result['title']);
			}
		}
		
		// fill select-fields
		$selGroup = $form->getElement('group_uid');
		if($queryGroups->count() > 0)
		{
			foreach($queryGroups->toArray() AS $result)
			{
				$selGroup->addMultiOption($result['uid'], $result['title']);
			}
		}
		
		// fill root_uid-fields
		$selBoards 	= $form->getElement('root_uid');
		$uid		= array();
		
		if($queryCategories->count() > 0 )
		{
			foreach($queryCategories->toArray() AS $cResult)
			{
				$selBoards->addMultiOption('cat_' . $cResult['uid'], $cResult['title']);
				$uid[] 			= 'cat_' . $cResult['uid'];
				
				$queryBoards	= $boards->fetchAll('root_uid = 0 AND category_uid = ' . $cResult['uid']);
				
				if($queryBoards->count() > 0)
				{
					foreach($queryBoards->toArray() AS $result)
					{
						$selBoards->addMultiOption($result['uid'], '- ' . $result['title']);
					}
				}
			}
			
			$selBoards->setAttrib('disable', $uid);
		}

		if ($this->_request->isPost() && $form->isValid($formData))
		{
			$form->removeElement('submit');
			
			if($boards->insert($form->getValues()))
			{
				$this->_redirect('/backend/boards.html');
			}
		}
		else
		{
			$form->populate($formData);
		}

		$this->view->form = $form;
	}
	
	
	
	/*
	 * Edit a boad
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function editAction()
	{
		// vars
		$board_uid	= $this->_request->getParam('uid', 0);
		$formData 	= $this->_request->getPost();
		
		// db
		$boards		= new Boards();
		$groups		= new Groups();
		$categories	= new Categories();
		$threads	= new Threads();
		$posts		= new Posts();
		
		// query
		$queryGroups		= $groups->fetchAll();
		$queryCategories	= $categories->fetchAll();
		
		// form
		$form	= new BoardsForm();
		
		if($board_uid)
		{
			$getBoard 	= $boards->find($board_uid);
			$resBoard	= $getBoard->toArray();
			
			if($getBoard->count() > 0)
			{
				// fill select-fields
				$selCategory = $form->getElement('category_uid');
				
				if($queryCategories->count() > 0)
				{
					foreach($queryCategories->toArray() AS $result)
					{
						$selCategory->addMultiOption($result['uid'], $result['title']);
					}
				}
				
				// fill group-fields
				$selGroup = $form->getElement('group_uid');
				
				if($queryGroups->count() > 0)
				{
					foreach($queryGroups->toArray() AS $result)
					{
						$selGroup->addMultiOption($result['uid'], $result['title']);
					}
				}
				
				// fill root_uid-fields
				$selBoards 	= $form->getElement('root_uid');
				$uid		= array();
				
				if($queryCategories->count() > 0 )
				{
					foreach($queryCategories->toArray() AS $cResult)
					{
						$selBoards->addMultiOption('cat_' . $cResult['uid'], $cResult['title']);
						$uid[] 			= 'cat_' . $cResult['uid'];
						
						$queryBoards	= $boards->fetchAll('root_uid = 0 AND category_uid = ' . $cResult['uid']);
						
						if($queryBoards->count() > 0)
						{
							foreach($queryBoards->toArray() AS $result)
							{
								$selBoards->addMultiOption($result['uid'], '- ' . $result['title']);
							}
						}
					}
					
					$selBoards->setAttrib('disable', $uid);
				}

				$form->populate($resBoard[0]);
			}
		}
		
		if ($this->_request->isPost() && $form->isValid($formData))
		{
			if($formData['is_deleted'] == 1)
			{
				$boards->delete('uid=' . $board_uid);
				$this->_redirect('/backend/boards.html');
			}
			elseif($formData['dump'] == 1)
			{
				$getThreads = $threads->fetchAll('board_uid =' . $board_uid);
				if($getThreads->count() > 0)
				{
					foreach($getThreads->toArray() AS $result)
					{
						$posts->delete('thread_uid=' . $result['uid']);
					}
				}
				$threads->delete('board_uid =' . $board_uid);
				
				$boards->update(array(
					'last_thread_uid' 	=> 0,
					'last_post_uid'		=> 0,
					'count_threads'		=> 0,
					'count_posts'		=> 0
				), 'uid=' . $board_uid);
			}
			
			$form->removeElement('dump');
			$form->removeElement('submit');
			if($boards->update($form->getValues(), 'uid=' . $board_uid))
			{
				$this->_redirect('/backend/board-' . $board_uid . '.html');
			}
		}
		
		$this->view->form = $form;
	}
}
?>